package morenoapp.components.thinkgraphv2.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import morenoapp.MorenoApp;
import morenoapp.model.view2.MorenoView2Face;
import morenoapp.model.view.listeners.MorenoViewChangeListener;
import morenoapp.model.view2.MorenoView2Data;
import morenoapp.util.I18N;
import org.jdesktop.application.Action;

/**
 *
 * @author dmitry.mamonov
 */
public class CommonDisplayConfigJPanel extends javax.swing.JPanel
        implements FirePropertyChange {
    MorenoView2Face config;
    MorenoViewChangeListener configListenr = new ConfigListener(this);
    ListSelectionListener listSelectionListener = new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()==false){
                //only summary result is used
                int selectedIndex = jListConfigurations.getSelectedIndex();//e.getFirstIndex();
                System.out.println("FIRST = "+selectedIndex+"/"+e.getLastIndex()+" CURRENT "+config.getCurrentView());
                if (selectedIndex>=0 && selectedIndex!=config.getCurrentView()){
                    config.setCurrentView(selectedIndex);
                }
            }
        }
    };
    
    /** Creates new form CommonConfigJPanel */
    public CommonDisplayConfigJPanel(MorenoView2Face _config) {
        this.config = _config;
        initComponents();
        postInitComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPaneConfigurations = new javax.swing.JScrollPane();
        jListConfigurations = new javax.swing.JList();
        jLabelConfiguration = new javax.swing.JLabel();
        jButtonAdd = new javax.swing.JButton();
        jButtonRemove = new javax.swing.JButton();
        jButtonSaveAsImage = new javax.swing.JButton();
        jLabelDiameter = new javax.swing.JLabel();
        jSliderDiameter = new javax.swing.JSlider();

        setName("Form"); // NOI18N

        jScrollPaneConfigurations.setName("jScrollPaneConfigurations"); // NOI18N

        jListConfigurations.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jListConfigurations.setName("jListConfigurations"); // NOI18N
        jScrollPaneConfigurations.setViewportView(jListConfigurations);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(CommonDisplayConfigJPanel.class);
        jLabelConfiguration.setText(resourceMap.getString("jLabelConfiguration.text")); // NOI18N
        jLabelConfiguration.setName("jLabelConfiguration"); // NOI18N

        jButtonAdd.setText(resourceMap.getString("jButtonAdd.text")); // NOI18N
        jButtonAdd.setName("jButtonAdd"); // NOI18N

        jButtonRemove.setText(resourceMap.getString("jButtonRemove.text")); // NOI18N
        jButtonRemove.setName("jButtonRemove"); // NOI18N

        jButtonSaveAsImage.setText(resourceMap.getString("jButtonSaveAsImage.text")); // NOI18N
        jButtonSaveAsImage.setName("jButtonSaveAsImage"); // NOI18N

        jLabelDiameter.setText(resourceMap.getString("jLabelDiameter.text")); // NOI18N
        jLabelDiameter.setName("jLabelDiameter"); // NOI18N

        jSliderDiameter.setMaximum(250);
        jSliderDiameter.setMinimum(25);
        jSliderDiameter.setMinorTickSpacing(25);
        jSliderDiameter.setPaintLabels(true);
        jSliderDiameter.setPaintTicks(true);
        jSliderDiameter.setName("jSliderDiameter"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelConfiguration)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelDiameter)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSliderDiameter, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPaneConfigurations, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jButtonAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonSaveAsImage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonRemove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(jLabelConfiguration)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonRemove)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSaveAsImage))
                    .addComponent(jScrollPaneConfigurations, 0, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelDiameter, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSliderDiameter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonRemove;
    private javax.swing.JButton jButtonSaveAsImage;
    private javax.swing.JLabel jLabelConfiguration;
    private javax.swing.JLabel jLabelDiameter;
    private javax.swing.JList jListConfigurations;
    private javax.swing.JScrollPane jScrollPaneConfigurations;
    private javax.swing.JSlider jSliderDiameter;
    // End of variables declaration//GEN-END:variables

    @Override
    public void firePropertyChangePublic(String property, Object oldValue, Object newValue) {
        firePropertyChange(property, oldValue, newValue);
    }

    @Action
    public void actionAddView() {
        String name = JOptionPane.showInputDialog(this, I18N.get("common_display_config.input_view_name"));
        if (name != null) {
            MorenoApp.getModel().getView().addView(name, true);
        }
    }

    @Action
    public void actionRemoveCurrentView() {
        if (config.getCurrentView() > 0) {
            config.deleteView(config.getCurrentView());
        } else {
            JOptionPane.showMessageDialog(this, I18N.get("common_display_config.root_view_can_not_be_deleted"));
        }
    }

    @Action
    public void actionSaveCurrentViewAsImage() {
        throw new RuntimeException();
    }

    @Action
    public void actionUpdateViewsList() {
        Vector<String> items = new Vector<String>();
        int size = config.getViewsCount();
        for (int index = 0; index < size; index++) {
            items.add(config.getViewName(index));
        }
        jListConfigurations.removeListSelectionListener(listSelectionListener);
        jListConfigurations.setListData(items);
        jListConfigurations.setSelectedIndex(config.getCurrentView());
        jListConfigurations.addListSelectionListener(listSelectionListener);
    }

    private void postInitComponents() {
        config.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                String name = evt.getPropertyName();
                if (MorenoView2Data.PROPERTY_VIEWNAME.equals(name)){
                    actionUpdateViewsList();
                }
            }
        });
        jListConfigurations.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        jListConfigurations.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount()==2){
                    String name = JOptionPane.showInputDialog(jListConfigurations,
                            I18N.get("common_display_config.change_view_name"),
                            config.getViewName());
                    if (name!=null){
                        config.setViewName(name);
                    }
                }
            }
        });

        jButtonAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionAddView();
            }
        });
        jButtonRemove.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionRemoveCurrentView();
            }
        });
        jButtonSaveAsImage.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionSaveCurrentViewAsImage();
            }
        });

       { //diameter component
            jSliderDiameter.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    if (config.getRingStep()!=jSliderDiameter.getValue()){
                        config.setRingStep(jSliderDiameter.getValue());
                    }
                }
            });
            config.addPropertyChangeListener(new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (MorenoView2Data.PROPERTY_RINGSTEP.equals(evt.getPropertyName())){
                        int newValue = (Integer)evt.getNewValue();
                        if (jSliderDiameter.getValue()!=newValue){
                            jSliderDiameter.setValue(newValue);
                        }
                    }
                }
            });
        }
    }
}
